<?php

class Application_Model_Mapper_HafenMapper{
	protected $_dbTable;

	public function setDbTable ($dbTable){
		if (is_string($dbTable)){
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract){
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable (){
		if (null === $this->_dbTable){
			$this->setDbTable('Application_Model_DbTable_Hafen');
		}
		return $this->_dbTable;
	}

	public function addHafen (Application_Model_Hafen $object){
		$now = new DateTime();
		/**
		 * Fill data array
		 */
		
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
			$data = array(
					'id' =>$generatedid,
					'created' => date('Y-m-d H:i:s'),
					'name' => $object->getName(),
					'lastupdater'=> $object->getLastupdater(), //id von ersteller mitschicken
					'lastupdated'=> date('Y-m-d H:i:s'),
					'lat' =>$object->getLat(),
					'lng' =>$object->getLng(),
					'tiefe' => $object->getTiefe(),
					'ankerplaetze' => $object->getAnkerplaetze(),
					'revier' => $object->getRevier(),
					'land' => $object -> getLand(),
					'stadt' => $object->getStadt(),
					'adresse' => $object->getAdresse(),
					'typ' => $object->getTyp(),
					'tankstelle' => $object->getTankstelle(),
					'parken' => $object->getParken(),
					'wasser' => $object->getWasser(),
					'strom' => $object->getStrom(),
					'toilette' => $object->getToilette(),
					'dusche' => $object->getDusche(),
					'reperatur' => $object->getReperatur(),
					'kran' => $object->getKran(),
					'slipping' => $object->getSlipping(),
					'supermarkt' => $object->getSupermarkt(),
					'waescherei' => $object->getWaescherei(),
					'travelLift' => $object->getTravelLift(),
					'wlan' => $object->getWlan(),
					'weblink' => $object->getWeblink(),
	// 				'koordinaten' => $object->getKoordinaten(),
// 					'premium' => $object->getPremium()
			// 				'profilbild' => $object->getProfilbild()
			);
			return $this->getDbTable()->insert($data);
		}
		else{
// 			$this->addHafen($object);
			$this->addHafen($object);
		}
	}

	
	public function search ($name, $tiefe, $ankerplaetze, $revier, $land, $stadt, $adresse, $typ, $tankstelle, $parken, $wasser, $strom, $toilette, $dusche, $reperatur, $kran, $slipping, $supermarkt, $waescherei, $travelLift, $wlan, $weblink, $lat, $lng, $formData){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('hafen');

		if($name){
			$entry->where('hafen.name like "%' . $formData['name'] . '%"');
		}
		if($tiefe){
			$entry->where('hafen.tiefe = ?', $formData['tiefe']);
		}
		if($ankerplaetze){
			$entry->where('hafen.ankerplaetze = ?', $formData['ankerplaetze']);
		}
		if($revier){
			$entry->where('hafen.revier = ?', $formData['revier']);
		}
		if($land){
			$entry->where('hafen.land = ?', $formData['land']);
		}
		if($stadt){
			$entry->where('hafen.stadt = ?', $formData['stadt']);
		}
		if($adresse){
			$entry->where('hafen.adresse like "%' . $formData['adresse'] .'%');
		}
		if($typ){
			$entry->where('hafen.typ = ?', $formData['typ']);
		}
		if($tankstelle){
			$entry->where('hafen.tankstelle = ?', $formData['tankstelle']);
		}
		if($parken){
			$entry->where('hafen.parken = ?', $formData['parken']);
		}
		if($wasser){
			$entry->where('hafen.wasser = ?', $formData['wasser']);
		}
		if($strom){
			$entry->where('hafen.strom = ?', $formData['strom']);
		}
		if($toilette){
			$entry->where('hafen.toilette = ?', $formData['toilette']);
		}
		if($dusche){
			$entry->where('hafen.dusche = ?', $formData['dusche']);
		}
		if($reperatur){
			$entry->where('hafen.reperatur = ?', $formData['reperatur']);
		}
		if($kran){
			$entry->where('hafen.kran = ?', $formData['kran']);
		}
		if($slipping){
			$entry->where('hafen.slipping = ?', $formData['slipping']);
		}
		if($supermarkt){
			$entry->where('hafen.supermarkt = ?', $formData['supermarkt']);
		}
		if($waescherei){
			$entry->where('hafen.waescherei = ?', $formData['waescherei']);
		}
		if($travelLift){
			$entry->where('hafen.travelLift = ?', $formData['travelLift']);
		}
		if($wlan){
			$entry->where('hafen.wlan = ?', $formData['wlan']);
		}
		if($weblink){
			$entry->where('hafen.weblink = ?', $formData['weblink']);
		}
		if($lat){
			$entry->where('hafen.lat = ?', $formData['lat']);
		}
		if($lng){
			$entry->where('hafen.lng = ?', $formData['lng']);
		}
		return $entry->query()->fetchall();
	}
	
	public function updateHafenStatus (Application_Model_Hafen $object){
		$data = array(
				'name' => $object->getName(),
				'lastupdater'=> $object->getLastupdater(),
				'lastupdated'=> date('Y-m-d H:i:s'),
				'lat' =>$object->getLat(),
				'lng' =>$object->getLng(),
				'tiefe' => $object->getTiefe(),
				'ankerplaetze' => $object->getAnkerplaetze(),
				'revier' => $object->getRevier(),
				'land' => $object -> getLand(),
				'stadt' => $object->getStadt(),
				'adresse' => $object->getAdresse(),
				'typ' => $object->getTyp(),
				'tankstelle' => $object->getTankstelle(),
				'parken' => $object->getParken(),
				'wasser' => $object->getWasser(),
				'strom' => $object->getStrom(),
				'toilette' => $object->getToilette(),
				'dusche' => $object->getDusche(),
				'reperatur' => $object->getReperatur(),
				'kran' => $object->getKran(),
				'slipping' => $object->getSlipping(),
				'supermarkt' => $object->getSupermarkt(),
				'waescherei' => $object->getWaescherei(),
				'travelLift' => $object->getTravelLift(),
				'wlan' => $object->getWlan(),
				'weblink' => $object->getWeblink(),
// 				'profilbild' => $object->getProfilbild()
		);
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?' , $object->getId());
		$table->update($data, $where);
	}

	public function deleteHafen ($id){
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);
	}

	public function fetchID ($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('hafen');
		$entry->where('hafen.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchAll(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('hafen');
		return $entry->query()->fetchall();
	}
	
	public function getalllaender(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$select = new Zend_Db_Select($db);
		$select->from('hafen');
		$select->where('land IS NOT NULL')->group('land');
		return $select->query()->fetchall();	
	}	
	
	public function getallstaedte(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$select = new Zend_Db_Select($db);
		$select->from('hafen');
		$select->where('stadt IS NOT NULL')->group('stadt');
		return $select->query()->fetchall();
	}	
	
	public function getallhaefen(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$select = new Zend_Db_Select($db);
		$select->from('hafen');
		$select->where('Name IS NOT NULL');
		return $select->query()->fetchall();	
	}

	public function getrandomhafenbyland($land){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('hafen');
		$entry->where('hafen.land = ?', $land);
		$entry->order('RAND()');
		return $entry->query()->fetch();
	}

	public function getrandomhafenbystadt($stadt){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('hafen');
		$entry->where('hafen.stadt = ?', $stadt);
		$entry->order('RAND()');
		return $entry->query()->fetch();
	}
}